﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using FooFun.DataElements.Dtos;
using FAQFrameworks;
using System.Web.UI.WebControls;

namespace FooFun.Web.Controls
{
    [ToolboxData("<{0}:SkinedControl runat=\"server\" />")]
    public class SkinedControl : TemplateControl, IDataElementBinder
    {
        protected override void OnInit(EventArgs e)
        {
            Page.Trace.Warn("BeginInit_" + this.Skin);

            base.OnInit(e);

            string path = GetSkinPath();

            ctr = (DataElementControl)this.LoadControl(path);

            this.Controls.Add(ctr);

            Page.Trace.Warn("EndInit_" + this.Skin);
        }

        protected override void AddParsedSubObject(object obj)
        {
            return;
        }

        DataElementControl ctr;

        string GetSkinPath()
        {

            string pathFormat = "~/Themes/{0}/{1}.ascx";

            string path
                = string.Format(pathFormat, this.Theme, this.Skin);

            string realPath = Page.Request.MapPath(path);

            if (!File.Exists(realPath))
            {
                path = string.Format
                    (pathFormat, ThemeConfig.DefaultTheme, this.Skin);
            }

            return path;
        }

        string skin = null;
        public string Skin
        {
            get
            {
                if (String.IsNullOrEmpty(skin))
                {
                    throw new Exception("没有设置皮肤");
                }

                return skin;
            }
            set
            {
                skin = value;
            }
        }

        string theme;
        public string Theme
        {
            get
            {
                return ThemeConfig.Theme;
            }
            set
            {
                theme = value;
            }
        }

        #region IDataElementBinder 成员

        public IDataElement DataElement
        {
            get
            {
                return ctr.DataElement;
            }
            set
            {
                ctr.DataElement = value;
            }
        }

        #endregion
    }
}
